package com.cgtong.android.mvp.presenter;

import com.cgtong.android.bean.DayReportBean;
import com.cgtong.android.bean.MonthYearReportBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.mvp.contract.ProblemCountContract;
import com.cgtong.android.mvp.model.ProblemCountModel;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * Created by chunc on 2017/10/25.
 */

public class ProblemCountPresenter implements ProblemCountContract.Presenter {

    private ProblemCountContract.Model mModle;
    private ProblemCountContract.View mView;

    public ProblemCountPresenter(ProblemCountContract.View view) {
        this.mView = view;
        mModle = new ProblemCountModel();
    }

    @Override
    public void httpDayReportData() {
        if(mView!=null){
            mView.setRefreshing(true);
        }
        mModle.httpDayReportData(new BaseCallBack<List<DayReportBean>>() {
            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.onError(e);
                }
            }

            @Override
            public void onSuccess(List<DayReportBean> dayReportBeen) {
                if (mView != null) {
                    mView.setRefreshing(false);
                    mView.onHttpDayReportDataSuccess(dayReportBeen);
                }
            }
        });
    }

    @Override
    public void httpMonthReportData(String beginMonth, final boolean isRefresh) {
        if (isRefresh) {
            mView.setEnableLoadMore(false);
            mView.setRefreshing(true);
        }
        mModle.httpMonthReportData(beginMonth, new BaseCallBack<List<MonthYearReportBean>>() {
            @Override
            public void onSuccess(List<MonthYearReportBean> monthYearReportBeen) {
                if (mView != null) {
                    if (isRefresh) {
                        mView.setRefreshing(false);
                        mView.setEnableLoadMore(true);
                        if (monthYearReportBeen == null || monthYearReportBeen.size() == 0) {
                            mView.onNullData();
                            return;
                        } else if (monthYearReportBeen.size() < ConstantManager.PROBLEMS_MAX_MONTH_REPORT_COUNT) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                        mView.onHttpMonthReportDataSuccess(monthYearReportBeen, isRefresh);
                    } else {
                        mView.onHttpMonthReportDataSuccess(monthYearReportBeen, isRefresh);
                        if (monthYearReportBeen.size() < ConstantManager.PROBLEMS_PAGE_SIZE) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                    }
                }
            }


            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.onError(e);
                }
            }
        });
    }


    @Override
    public void httpYearReportData() {
        mModle.httpYearReportData(new BaseCallBack<List<MonthYearReportBean>>() {
            @Override
            public void onSuccess(List<MonthYearReportBean> monthYearReportBeen) {
                if (mView != null) {
                    mView.setRefreshing(false);
                    mView.onHttpYearReportDataSuccess(monthYearReportBeen);
                }
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.onError(e);
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        if (mView != null) {
            mView = null;
        }
    }
}
